﻿Imports System.Data.OleDb
Partial Public Class addBook
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub
    Function getFileExt(ByVal fn As String)
        Dim fileParts = fn.Split(".")
        Dim suffix = fileParts(fileParts.Length - 1)
        Return "." & suffix
    End Function

    Protected Sub btn_submission_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_submission.Click

        Dim myGUID = Guid.NewGuid()
        Dim newFileName As String = myGUID.ToString() & getFileExt(fu_picture.FileName)
        Dim fileLocationOnServerHardDisk = Request.MapPath("img/thumb") & "/" & newFileName
        fu_picture.SaveAs(fileLocationOnServerHardDisk)
        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("BookMeetConnString").ConnectionString)
        Dim SqlString As String = "Insert into booklist(Title,Author,PublicationDate,Pages,Publisher,Blurb,imgurl,AverageRating) Values (@f1,@f2,@f3,@f4,@f5,@f6,@f7,@f8)"
        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@f1", tb_booktitle.Text)
        cmd.Parameters.AddWithValue("@f2", tb_bookauthor.Text)
        cmd.Parameters.AddWithValue("@f3", tb_bookpubyear.Text)
        cmd.Parameters.AddWithValue("@f4", tb_bookpages.Text)
        cmd.Parameters.AddWithValue("@f5", tb_publisher.Text)
        cmd.Parameters.AddWithValue("@f6", tb_blurb.Text)
        cmd.Parameters.AddWithValue("@f7", If(fu_picture.HasFile, "img/thumb/" & newFileName, DBNull.Value))
        cmd.Parameters.AddWithValue("@f8", rbl_Stars.SelectedValue)
        oleDbConn.Open()
        cmd.ExecuteNonQuery()
        System.Threading.Thread.Sleep("2000")
        Response.Redirect("~/addedrecord.aspx")

    End Sub
    
    Protected Sub rbl_Stars_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles rbl_Stars.SelectedIndexChanged

    End Sub
End Class
